package com.honghu.villagepost.activity;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.honghu.villagepost.MainActivity;
import com.honghu.villagepost.R;
import com.honghu.villagepost.base.BaseActivity;
import com.honghu.villagepost.constant.SputilsConstant;
import com.honghu.villagepost.dialog.AgreementDialog;
import com.tbruyelle.rxpermissions3.RxPermissions;

/**
 * Created by mqwang on 2017/9/28.
 */

public class SplashActivity extends BaseActivity {

    String[] perms = {
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
//            Manifest.permission.ACCESS_BACKGROUND_LOCATION, //target为Q时，动态请求后台定位权限
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setStatusBarTintResource(R.color.transparent);

        boolean showAgree = SPUtils.getInstance().getBoolean(SputilsConstant.SHOW_AGREE_DIALOG, true);
        if (showAgree){
            AgreementDialog agreementDialog = AgreementDialog.newInstance();
            agreementDialog.setAgreementDialogListener(new AgreementDialog.AgreementDialogListener() {
                @Override
                public void onAgree() {
                    SPUtils.getInstance().put(SputilsConstant.SHOW_AGREE_DIALOG, false);
                    RxPermissions rxPermissions = new RxPermissions(SplashActivity.this); // where this is an Activity or Fragment instance
                    rxPermissions
                            .request(perms)
                            .subscribe(granted -> {
                                jumpMain(0);
                            });
                }

                @Override
                public void onDisAgree() {
                    finish();
                }
            });
            agreementDialog.show(getSupportFragmentManager());
        }else {
            jumpMain(2000);
        }
    }

    private void jumpMain(long delay) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ActivityUtils.startActivity(SplashActivity.this, MainActivity.class);
                finish();
            }
        }, delay);
    }

    @Override
    protected boolean isNeedTitleBar() {
        return false;
    }
}
